package com.ggh.jinjilive.ui.mall.goods.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemAddSpecificationBinding;
import com.ggh.jinjilive.databinding.ItemImageViewBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.mall.goods.GoodsSpecificationMobile;
import com.huantansheng.easyphotos.models.album.entity.Photo;

import java.util.List;

public class AddSpecificationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<GoodsSpecificationMobile> goodsSpecificationMobileList;

    public void setAdd(List<GoodsSpecificationMobile> goodsSpecificationMobileList) {
        this.goodsSpecificationMobileList = goodsSpecificationMobileList;
        notifyDataSetChanged();
    }

    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemAddSpecificationBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_add_specification, parent, false);
        return new ItemViewHolder(binding);
    }

    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder recyclerViewholder,final int position) {
       ItemViewHolder holder = (ItemViewHolder) recyclerViewholder;
        GoodsSpecificationMobile goodsSpecificationMobile = goodsSpecificationMobileList.get(position);
        if(null != goodsSpecificationMobile){
            //规格
            holder.binding.etSpecification.setText(goodsSpecificationMobile.getName());
            holder.binding.etSpecification.setEnabled(false);
            //库存
            holder.binding.etInventory.setText(goodsSpecificationMobile.getStock() +"");
            holder.binding.etInventory.setEnabled(false);
            //价格
            holder.binding.etPrice.setText(goodsSpecificationMobile.getFee() +"");
            holder.binding.etPrice.setEnabled(false);
            if(holder.binding.delete.getVisibility() == View.VISIBLE){
                //规格
                holder.binding.etSpecification.setEnabled(true);
                //库存
                holder.binding.etInventory.setEnabled(true);
                //价格
                holder.binding.etPrice.setEnabled(true);
            }
            //编辑
            holder.binding.edit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(holder.binding.delete.getVisibility() == View.VISIBLE){
                        if(holder.binding.edit.getText().toString().equals("完成")){
                            goodsSpecificationMobileList.get(position).setName( holder.binding.etSpecification.getText().toString());
                            goodsSpecificationMobileList.get(position).setFee(Double.valueOf( holder.binding.etPrice.getText().toString()));
                            goodsSpecificationMobileList.get(position).setStock(Integer.valueOf(holder.binding.etInventory.getText().toString()));
                            holder.binding.edit.setText("编辑");
                            holder.binding.etSpecification.setEnabled(false);
                            holder.binding.etInventory.setEnabled(false);
                            holder.binding.etPrice.setEnabled(false);
                            holder.binding.delete.setVisibility(View.GONE);
                        }
                        if(null != addItemOnClickListener){
                            addItemOnClickListener.onSkipClicked(999999,goodsSpecificationMobileList);
                        }
                    }else {
                        holder.binding.edit.setText("完成");
                        holder.binding.etSpecification.setEnabled(true);
                        holder.binding.etInventory.setEnabled(true);
                        holder.binding.etPrice.setEnabled(true);
                        holder.binding.delete.setVisibility(View.VISIBLE);
                    }
                }
            });
            //删除
            holder.binding.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(position,"删除");
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == goodsSpecificationMobileList ? 0 : goodsSpecificationMobileList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemAddSpecificationBinding binding;
        public ItemViewHolder(@NonNull ItemAddSpecificationBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}